/* ====================================================================
 * (#)Header: RuleReaderProviderDrools.java
 * (#)Revision: 
 * (#)Date Created: Sep 30, 2011 12:47:15 AM
 * (#)Created By: Karol Wozniak
 * ====================================================================
 *
 * Copyright (c) 2007 karolak .  All rights reserved.
 *
 * ====================================================================
 * 
 * The software application represented here is the sole property of 
 * karolak. Its unauthorized utilization is expressly prohibited.
 * 
 * ====================================================================
 */
package com.karolak.rules.drools;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import org.drools.decisiontable.InputType;
import org.drools.decisiontable.SpreadsheetCompiler;

import com.karolak.rules.RuleFileType;
import com.karolak.rules.RuleReaderProvider;

/**
 * <code>RuleReaderProviderDrools</code> is used to ....
 * 
 * @author <a href="mailto:kwozniak@hotmail.com">Karol Wozniak</a>
 */
public class RuleReaderProviderDrools implements RuleReaderProvider {

	public Map<?, ?> createProperties() {
		Map<?, ?> map = new HashMap<Object, Object>();
		// map.put("source", "drl");
		return map;
	}

	public Reader create(StringBuilder builder, RuleFileType fileType) {
		if (RuleFileType.CSV.equals(fileType)) {
			String str = convertXls(InputType.CSV, new ByteArrayInputStream(builder.toString().getBytes()), "");
			return new StringReader(str);
		}
		return new StringReader(builder.toString());
	}

	public Reader create(String fileName, RuleFileType fileType) {
		return create(fileName, fileType, null);
	}

	public Reader create(String fileName, RuleFileType fileType, String sheetName) {
		InputStream is = getClass().getResourceAsStream(fileName);
		if (RuleFileType.CSV.equals(fileType)) {
			String ruleStr = convertXls(InputType.CSV, is, "");
			return new StringReader(ruleStr);
		}
		if (RuleFileType.XLS.equals(fileType)) {
			String ruleStr = convertXls(InputType.XLS, is, "");
			// System.out.println(ruleStr);
			return new StringReader(ruleStr);
		}
		if (RuleFileType.NATIVE.equals(fileType)) {
			return create(is);
		}
		if (fileType == null) {
			return create(is);
		}
		return create(is);
	}

	// === protected api === //
	protected Reader create(InputStream is) {
		return new InputStreamReader(is);
	}

	protected String convertXls(InputType inputType, InputStream is, String sheetName) {
		try {

			SpreadsheetCompiler spreadsheetCompiler = new SpreadsheetCompiler();
			// if (!StringUtils.isEmpty(sheet)) {
			if (sheetName != null && sheetName.trim().length() > 0) {
				return spreadsheetCompiler.compile(is, sheetName);
			}
			return spreadsheetCompiler.compile(is, inputType);
			// return spreadsheetCompiler.compile(is, InputType.CSV);
			// } catch (Exception ex) {
			// log.fatal(ex);
			// System.out.println(ex);
			// throw ex;
			// throw new RuntimeException(ex);
		} finally {
			try {
				if (is != null) {
					is.close();
				}
			} catch (Exception e) {
				// log.fatal(e);
				System.out.println(e);
				// throw e;
			}
		}
	}
}
